﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.AfterSale
{
    public partial class ASale_RePairChange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_CUTNAM.OnClientTriggerClick = Window1.GetSaveStateReference(hid_CUTNAM.ClientID, txt_CUTNAM.ClientID)
                  + Window1.GetShowReference("/PublicModel/SelectCustom.aspx", "选择客户");
                Load_Data();
                BindGrid();
            }
        }

        protected void Load_Data()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                int id = int.Parse(Request["id"]);
                AfSale_PrInfo Pr = TbsDbSession.Default.From<AfSale_PrInfo>().Where(AfSale_PrInfo._.ID == id).ToFirst();
                if (Pr != null)
                {
                    Drp_Status.SelectedValue = Pr.Status.Value.ToString();
                    hf.Text = Pr.ATTACH;
                    Lbl_CRDATE.Text = "日期：" + Pr.CRDATE.Value.ToString("yyyy-MM-dd");
                    txt_TELNUB.Text = Pr.TELNUB;
                    txt_OEMAIL.Text = Pr.OEMAIL;
                    txt_CUTNAM.Text = Pr.CUTNAM;
                    txt_ADDRES.Text = Pr.ADDRES;
                    txt_CUTTEL.Text = Pr.CUTTEL;
                    txt_CONTAT.Text = Pr.CONTAT;
                    txt_PRJECT.Text = Pr.PRJECT;
                    txt_SEDATE.Text = Pr.SEDATE.Value.ToString("yyyy-MM-dd");
                    txt_REDATE.Text = Pr.REDATE.Value.ToString("yyyy-MM-dd");
                    txt_REQDAT.Text = Pr.REQDAT.Value.ToString("yyyy-MM-dd");
                    txt_COMDAT.Text = Pr.COMDAT.Value.ToString("yyyy-MM-dd");
                    txt_ENGSIG.Text = Pr.ENGSIG;
                    txt_CUTSIG.Text = Pr.CUTSIG;
                    txt_ENGSIG.Enabled = false;
                    if (Pr.RESULT != null)
                    {
                        if (Pr.RESULT == "满意")
                        {
                            CheckBox1.Checked = true;
                            CheckBox2.Enabled = false;
                        }
                        else
                        {
                            txt_RESULTElse.Enabled = true;
                            CheckBox2.Checked = true;
                            CheckBox1.Enabled = false;
                            if (Pr.RESULT != "不满意")
                                txt_RESULTElse.Text = Pr.RESULT;
                        }
                    }
                    if (Pr.NATURE.Value)
                    {
                        CheckBox4.Checked = true;
                        CheckBox3.Enabled = false;
                        txt_Price.Enabled = true;
                        RList.Enabled = true;
                        if (Pr.ISPREM.Value)
                        {
                            RList.SelectedValue = "0";
                        }
                        else
                        {
                            RList.SelectedValue = "1";
                        }
                        if (Pr.AMOUNT.Value > 0)
                            txt_Price.Text = Pr.AMOUNT.Value.ToString();
                    }
                    else
                    {
                        CheckBox3.Checked = true;
                        CheckBox4.Enabled = false;
                    }
                    if (Pr.IFCHEK.Value)
                    {
                        Rdo_IFCHEK.SelectedValue = "0";
                    }
                    else
                    {
                        Rdo_IFCHEK.SelectedValue = "1";
                    }
                }
            }
            else
            {
                tr1.Visible = false;
                Lbl_CRDATE.Text = "日期：" + DateTime.Now.ToString("yyyy-MM-dd");
            }
        }



        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox1.Checked)
            {
                CheckBox2.Checked = false;
                CheckBox2.Enabled = false;
                txt_RESULTElse.Enabled = false;
            }
            else
            {
                CheckBox2.Checked = false;
                CheckBox2.Enabled = true;
                txt_RESULTElse.Enabled = false;
            }

        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox2.Checked)
            {
                CheckBox1.Checked = false;
                CheckBox1.Enabled = false;
                txt_RESULTElse.Enabled = true;
            }
            else
            {
                CheckBox1.Checked = false;
                CheckBox1.Enabled = true;
                txt_RESULTElse.Enabled = false;

            }
        }

        protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox3.Checked)
            {
                CheckBox4.Checked = false;
                CheckBox4.Enabled = false;
                txt_Price.Enabled = false;
                RList.Enabled = false;
            }
            else
            {
                CheckBox4.Checked = false;
                CheckBox4.Enabled = true;
                txt_Price.Enabled = false;
                RList.Enabled = false;
            }
        }

        protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckBox4.Checked)
            {
                CheckBox3.Checked = false;
                CheckBox3.Enabled = false;
                txt_Price.Enabled = true;
                RList.Enabled = true;
            }
            else
            {
                CheckBox3.Checked = false;
                CheckBox3.Enabled = true;
                txt_Price.Enabled = false;
                RList.Enabled = false;

            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string mess = "";
            bool yz = true;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                if (CheckBox1.Checked == false && CheckBox2.Checked == false)
                {
                    yz = false;
                    mess = "请选择维修结果！";
                }
            }
            if (CheckBox3.Checked == false && CheckBox4.Checked == false)
            {
                yz = false;
                mess = "请选择维修性质";
            }

            if (txt_TELNUB.Text == "")
            {
                mess = "请输入售后维修电话";
                yz = false;
                txt_TELNUB.MarkInvalid(mess);
            }


            if (txt_OEMAIL.Text == "")
            {
                mess = "请输入售后服务邮箱";
                yz = false;
                txt_OEMAIL.MarkInvalid(mess);
            }



            if (txt_CUTNAM.Text == "")
            {
                mess = "请输入客户名称";
                yz = false;
                txt_CUTNAM.MarkInvalid(mess);
            }


            if (txt_ADDRES.Text == "")
            {
                mess = "请输入地址";
                yz = false;
                txt_ADDRES.MarkInvalid(mess);
            }


            if (txt_CUTTEL.Text == "")
            {
                mess = "请输入电话";
                yz = false;
                txt_CUTTEL.MarkInvalid(mess);
            }


            if (txt_CONTAT.Text == "")
            {
                mess = "请输入联系人";
                yz = false;
                txt_CONTAT.MarkInvalid(mess);
            }



            if (txt_PRJECT.Text == "")
            {
                mess = "请输入维修项目";
                yz = false;
                txt_PRJECT.MarkInvalid(mess);
            }



            if (txt_SEDATE.Text == "")
            {
                mess = "请输入原送货安装日期";
                yz = false;
                txt_SEDATE.MarkInvalid(mess);
            }

            if (txt_REDATE.Text == "")
            {
                mess = "请输入通知维修日期";
                yz = false;
                txt_REDATE.MarkInvalid(mess);
            }
            if (txt_REQDAT.Text == "")
            {
                mess = "请输入要求完工期";
                yz = false;
                txt_REQDAT.MarkInvalid(mess);
            }

            if (txt_COMDAT.Text == "")
            {
                mess = "请输入实际完工期";
                yz = false;
                txt_COMDAT.MarkInvalid(mess);
            }

            if (txt_ENGSIG.Text == "")
            {
                mess = "请输入维修员签名";
                yz = false;
                txt_ENGSIG.MarkInvalid(mess);
            }






            if (yz)
            {
                #region 其他字段设定
                string result = "";
                if (CheckBox1.Checked)
                {
                    result = "满意";
                }
                else
                {
                    result = "不满意";
                    if (txt_RESULTElse.Text != "")
                    {
                        result = txt_RESULTElse.Text;
                    }
                }
                bool isNATURE = false;
                if (CheckBox4.Checked)
                {
                    isNATURE = true;
                }
                bool isPREM = false;
                if (RList.SelectedValue == "0")
                    isPREM = true;
                bool isCHEK = false;
                if (Rdo_IFCHEK.SelectedValue == "0")
                    isCHEK = true;
                decimal price = 0;
                if (txt_Price.Text != "")
                    price = Convert.ToDecimal(txt_Price.Text);
                #endregion

                int returnvalue = 0;
                AfSale_PrInfo Pr = new AfSale_PrInfo();
                if (!string.IsNullOrEmpty(Request["id"]))
                {
                    int id = int.Parse(Request["id"]);

                    Pr.ID = id;
                    Pr.Attach();
                    Pr.Adder = CookInfo.EAdminTrueName();
                    Pr.TELNUB = txt_TELNUB.Text;
                    Pr.OEMAIL = txt_OEMAIL.Text;
                    Pr.CUTNAM = txt_CUTNAM.Text;
                    Pr.ADDRES = txt_ADDRES.Text;
                    Pr.CUTTEL = txt_CUTTEL.Text;
                    Pr.CONTAT = txt_CONTAT.Text;
                    Pr.PRJECT = txt_PRJECT.Text;
                    Pr.SEDATE = Convert.ToDateTime(txt_SEDATE.Text);
                    Pr.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                    Pr.REQDAT = Convert.ToDateTime(txt_REQDAT.Text);
                    Pr.COMDAT = Convert.ToDateTime(txt_COMDAT.Text);
                    Pr.ENGSIG = txt_ENGSIG.Text;
                    Pr.CUTSIG = txt_CUTSIG.Text;
                    Pr.RESULT = result;
                    Pr.NATURE = isNATURE;
                    Pr.AMOUNT = price;
                    Pr.ISPREM = isPREM;
                    Pr.IFCHEK = isCHEK;
                    Pr.AddTime = DateTime.Now;
                    Pr.ATTACH = hf.Text;
                    Pr.Status =int.Parse(Drp_Status.SelectedValue);
                    returnvalue = TbsDbSession.Default.Update<AfSale_PrInfo>(Pr);
                }
                else
                {
                    Pr.DeAttach();
                    Pr.REPRID = DFieldSN.SN(EnumData.DataSNEunm.维修记录流水号);
                    Pr.CRDATE = DateTime.Now;
                    Pr.TELNUB = txt_TELNUB.Text;
                    Pr.OEMAIL = txt_OEMAIL.Text;
                    Pr.CUTNAM = txt_CUTNAM.Text;
                    Pr.ADDRES = txt_ADDRES.Text;
                    Pr.CUTTEL = txt_CUTTEL.Text;
                    Pr.CONTAT = txt_CONTAT.Text;
                    Pr.PRJECT = txt_PRJECT.Text;
                    Pr.SEDATE = Convert.ToDateTime(txt_SEDATE.Text);
                    Pr.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                    Pr.REQDAT = Convert.ToDateTime(txt_REQDAT.Text);
                    Pr.COMDAT = Convert.ToDateTime(txt_COMDAT.Text);
                    Pr.ENGSIG = txt_ENGSIG.Text;
                    Pr.CUTSIG = txt_CUTSIG.Text;
                    //Pr.RESULT = result;
                    Pr.NATURE = isNATURE;
                    Pr.AMOUNT = price;
                    Pr.ISPREM = isPREM;
                    Pr.IFCHEK = isCHEK;
                    //Pr.TAXNUB = txt_TAXNUB.Text;
                    Pr.AddTime = DateTime.Now;
                    Pr.Adder = CookInfo.EAdminTrueName();
                    Pr.ATTACH = hf.Text;
                    Pr.Status = int.Parse(Drp_Status.SelectedValue);
                    returnvalue = TbsDbSession.Default.Insert<AfSale_PrInfo>(Pr);
                }
                if (returnvalue > 0)
                {
                    Alert.Show("记录保存成功！", "提示", ActiveWindow.GetHideRefreshReference());
                }
            }
            else
                Alert.Show(mess);
        }



        #region 文件上传
        List<TempFile> files = new List<TempFile>();
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            if (filePhoto.HasFile)
            {
                string fileName = filePhoto.ShortFileName;
                String[] str = { "1", "2", "3", "4", "5", "6", "7" };
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
                hf.Text += fileName + ";";
                // 清空文件上传组件
                filePhoto.Reset();
                BindGrid();
            }
        }

        protected void BindGrid()
        {
            List<string> ls = StringHelper.GetSubStringList(hf.Text, ';');
            foreach (string item in ls)
            {
                TempFile tf = new TempFile();
                tf.FileName = item;
                files.Add(tf);
            }
            Grid1.DataSource = files;
            Grid1.DataBind();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string[] str = Grid1.Rows[e.RowIndex].Values;
                hf.Text = hf.Text.Replace(str[1], "");
                BindGrid();
            }
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[0] = "<a href=\"/upload/" + e.Values[0] + "\">" + e.Values[0] + "</a>";
        }
        #endregion
    }
}